﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 登陆界面
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void txtUerName_Leave(object sender, EventArgs e)
        {
            string uid = textBox1.Text.Trim();
            if (uid.Length == 0)
            {
                textBox1.ForeColor = Color.Red;
                textBox1.Text = "用户名未填写";
                return;
            }

            string connStr = @"server=.;database=MyDataBase01;uid=sa;pwd=123456;";
            string sql = "select COUNT(*) from ADO.LoginTbl where loginUid='" + uid + "';";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    int res = (int)cmd.ExecuteScalar();
                    if (res > 0)
                    {
                        textBox1.ForeColor = Color.Red;
                        textBox1.Text = "用户名已存在！";
                    }
                    else
                    {
                        textBox1.ForeColor = Color.Green;
                        textBox1.Text = "用户名可用";
                    }
                }

            }

        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            string uid = textBox1.Text.Trim();
            string pwd1 = textBox2.Text;

            if (uid.Length == 0 || pwd1.Length == 0 )
            {
                MessageBox.Show("请填写完整信息");
                return;
            }

            

            if (textBox1.Text == "用户名已存在!")
            {
                MessageBox.Show("用户名已存在，请重新输入！");
                return;
            }


            string connStr = @"server=.;database=MyDataBase01;uid=sa;pwd=123456;";

            string sql = string.Format("insert into LoginTbl(loginUid,loginPwd) values('{0}','{1}');",
                uid,
                pwd1);
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    conn.Open();
                    int res = cmd.ExecuteNonQuery();
                    if (res > 0)
                    {
                        MessageBox.Show("恭喜你！注册成功！");
                    }
                    else
                    {
                        MessageBox.Show("注册失败！");
                    }
                }

            }


        }

        
    }
}